package web.type;

import bean.Type;
import bean.User;
import bean.Video;
import com.alibaba.fastjson.JSON;
import entity.TypeEntity;
import entity.UserEntity;
import entity.VideoEntity;
import util.FANREN;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "TypeDelete",urlPatterns = "/dao/update/typeDelete")
public class TypeDelete extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, RuntimeException {
        String jsonString = FANREN.getJson(request);
        System.out.println(jsonString);

        //删除一条记录
        EntityManager em = (EntityManager) request.getAttribute("_entity.Manager");
        Type type = JSON.parseObject(jsonString,Type.class);
        TypeEntity typeEntity = em.find(TypeEntity.class,Integer.parseInt(type.getTid()));

        Query query = em.createQuery("select v from VideoEntity v");
        List<VideoEntity> list = query.getResultList();
        for (int i=0; i<list.size(); i++) {
            if((list.get(i).getTid().getTid()+"").equals(typeEntity.getTid()+""))
                throw new RuntimeException();
        }


        if(typeEntity!=null)
            em.remove(typeEntity);
    }
}
